/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import gourmetgrossisten.Gourmetklient;
import gourmetgrossisten.ObserverListener;
import interfaces.BrugerInterface;
import interfaces.ConnectionInterface;
import interfaces.GourmetGrossistInterface;
import interfaces.RemoteActionInterface;
import interfaces.VareInterface;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import utility.PageHandler;

public class KlientGUI extends javax.swing.JFrame implements ActionListener {

    private PageHandler ph;
    private LogindPanel logindPanel;
    private VareBestillingsPanel vareBestillingsPanel;
    private GourmetGrossistInterface gourmetGrossistInterface;
    private ConnectionInterface connectionInterface;
    private ObserverListener observerListener;

    public KlientGUI() throws RemoteException {
        initComponents();
        setSize(1200, 800);
        observerListener = new ObserverListener(this);
        ph = new PageHandler((CardLayout) jPanel1.getLayout(), jPanel1);
        logindPanel = new LogindPanel(this);
        ph.addPage(logindPanel, logindPanel.getClass().getName());
        vareBestillingsPanel = new VareBestillingsPanel(this);
        ph.addPage(vareBestillingsPanel, vareBestillingsPanel.getClass().getSimpleName());
        System.out.println(vareBestillingsPanel.getClass().getSimpleName());
        try {
            gourmetGrossistInterface = Gourmetklient.getMainStub();
            System.out.println("Forbindelse oprettet");
        } catch (RemoteException ex) {
            Logger.getLogger(KlientGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(KlientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void logInd(String brugernavn, String password) {
        try {
            connectionInterface = gourmetGrossistInterface.getConnection(brugernavn, password);
            connectionInterface.setObserverListener(observerListener);
//            gourmetGrossistInterface.addActionListener(this);
            System.out.println("Connection oprettet");

        } catch (RemoteException ex) {
            System.out.println(ex.getLocalizedMessage());
        } catch (SQLException ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }

    public void showPage(String ref) {
        ph.show(ref);
    }

    public LogindPanel getLogindPanel() {
        return logindPanel;
    }

    public VareBestillingsPanel getVareBestillingsPanel() {
        return vareBestillingsPanel;
    }

    public ConnectionInterface getConnectionInterface() {
        return connectionInterface;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BannerPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(null);

        BannerPanel.setBackground(new java.awt.Color(102, 102, 102));
        BannerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        BannerPanel.setForeground(new java.awt.Color(0, 255, 0));

        jLabel1.setFont(new java.awt.Font("Kalinga", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 255, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("GourmetGrossisten");

        javax.swing.GroupLayout BannerPanelLayout = new javax.swing.GroupLayout(BannerPanel);
        BannerPanel.setLayout(BannerPanelLayout);
        BannerPanelLayout.setHorizontalGroup(
            BannerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, BannerPanelLayout.createSequentialGroup()
                .addContainerGap(247, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 711, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(238, 238, 238))
        );
        BannerPanelLayout.setVerticalGroup(
            BannerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getContentPane().add(BannerPanel);
        BannerPanel.setBounds(0, 0, 1200, 60);

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.CardLayout());
        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 60, 1200, 740);

        background.setBackground(new java.awt.Color(102, 102, 102));
        background.setOpaque(true);
        getContentPane().add(background);
        background.setBounds(0, 0, 1200, 800);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
//        try {
//            gourmetGrossistInterface.removeActionListener(this);
//            System.out.println("actionlisteneren er blevet fjernet fra serveren.");
//        } catch (RemoteException ex) {
//            JOptionPane.showMessageDialog(this, "Der er opstået en fejl på forbindelsen mod serveren", "Remote-Exception", 0);
//        }
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(KlientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(KlientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(KlientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(KlientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new KlientGUI().setVisible(true);
                } catch (RemoteException ex) {
                    System.out.println(ex.getLocalizedMessage());                 
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel BannerPanel;
    private javax.swing.JLabel background;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("OMG SOMETHING HAS HAPPENED");
        if (ae.getSource() instanceof VareInterface) {
            vareBestillingsPanel.opdaterDenneVare((VareInterface) ae.getSource());
        }else{
            System.out.println("Source er ikke et vareinterface?");
        }
    }
}
